<?php

/**
 * This is the model class for table "page".
 *
 * The followings are the available columns in table 'page':
 * @property integer $pag_code
 * @property string $pag_title
 * @property string $pag_name
 * @property string $pag_icon
 * @property string $pag_content
 * @property string $pag_type
 * @property integer $sit_code
 *
 * The followings are the available model relations:
 * @property BlogEntry[] $blogEntries
 * @property Contact[] $contacts
 * @property Gallery[] $galleries
 * @property New[] $news
 * @property Site $sitCode
 */
class Page extends CActiveRecord
{
	
  
  /**
	 * Returns the static model of the specified AR class.
	 * @return Page the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'page';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('pag_name,pag_title', 'required'),
			array('pag_code, sit_code', 'numerical', 'integerOnly'=>true),
			array('pag_title, pag_name, pag_icon', 'length', 'max'=>145),		
			array('pag_content', 'length', 'max'=>3000),
			array('pag_type', 'length', 'max'=>7),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('pag_code, pag_title, pag_name, pag_icon, pag_content, pag_type, sit_code', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'blogEntries' => array(self::HAS_MANY, 'BlogEntry', 'pag_code'),
			'contacts' => array(self::HAS_MANY, 'Contact', 'pag_code'),
			'galleries' => array(self::HAS_MANY, 'Gallery', 'pag_code'),
			'news' => array(self::HAS_MANY, 'New', 'pag_code'),
			'sitCode' => array(self::BELONGS_TO, 'Site', 'sit_code'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'pag_code' => 'Pag Code',
			'pag_title' => 'Pag Title',
			'pag_name' => 'Pag Name',
			'pag_icon' => 'Pag Icon',
			'pag_content' => 'Pag Content',
			'pag_type' => 'Pag Type',
			'sit_code' => 'Sit Code',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('pag_code',$this->pag_code);
		$criteria->compare('pag_title',$this->pag_title,true);
		$criteria->compare('pag_name',$this->pag_name,true);
		$criteria->compare('pag_icon',$this->pag_icon,true);
		$criteria->compare('pag_content',$this->pag_content,true);
		$criteria->compare('pag_type',$this->pag_type,true);
		$criteria->compare('sit_code',$this->sit_code);
		$site = (int)$_GET['site'];
    if($site != 0){
          $criteria->condition = 'sit_code='.$site;
      }else{
          throw new CHttpException(404, 'The requested page does not exist.');
      }

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	 public static function enumItem($model,$attribute)
  {
          $attr=$attribute;
          //self::resolveName($model,$attr);
          preg_match('/\((.*)\)/',$model->tableSchema->columns[$attr]->dbType,$matches);
          foreach(explode(',', $matches[1]) as $value)
          {
                  $value=str_replace("'",null,$value);
                  $values[$value]=Yii::t('enumItem',$value);
          }
          
          return $values;
  }
  
  public function obtenerRuta()
  {
    
    switch($this->pag_type)
    {
      case 'blog':
         return Yii::app()->createUrl("admin/blogEntry/admin",array("page"=>$this->pag_code));
      break;
       case 'gallery':
         return Yii::app()->createUrl("admin/gallery/admin",array("page"=>$this->pag_code));
      break;
      case 'news':
         return Yii::app()->createUrl("admin/newItem/admin",array("page"=>$this->pag_code));
      break;
      case 'contact':
         return Yii::app()->createUrl("admin/contact/admin",array("page"=>$this->pag_code));
      break;
    }        
  
  }
  
  
  public function isOwner($pageCode=0){
    $criteria=new CDbCriteria;
    $model=Page::model()->findByPk($pageCode);
    if($model){
      $criteria->condition = 'usr_code='.Yii::app()->user->id.' AND sit_code='.$model->sit_code;
    return (boolean)Site::model()->count($criteria);
    }
    return false;
  
  }
  
  
     
}